﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 保存按钮数据的类
/// </summary>
[CreateAssetMenu(menuName = "MyScript/Input/ButtonData")]
public class ButtonData : ScriptableObject
{
    protected bool m_pressFlag;

    protected ButtonEventArgs m_eventArgs;

    [SerializeField] protected string m_name;
    [SerializeField] protected GameTypeDefine.BUTTON_ID m_buttonId = GameTypeDefine.BUTTON_ID.NONE;

    public GameTypeDefine.BUTTON_ID ID
    {
        get
        {
            return m_buttonId;
        }
    }

    private float m_tempAxis;

    public void Init()
    {
        if (null == m_eventArgs)
        {
            m_eventArgs = new ButtonEventArgs(m_buttonId);
        }
    }

    public virtual bool Detect()
    {
        m_tempAxis = Input.GetAxis(m_name);

        if (m_tempAxis == 0 && !m_pressFlag)
        {
            return false;
        }

        m_eventArgs.Axis = m_tempAxis;

        if (m_tempAxis != 0 && !m_pressFlag)
        {
            m_pressFlag = true;
            EventMgr.InvokeEvent(GameTypeDefine.EVENT_ID.BUTTON_DOWN, m_eventArgs);
        }

        if (m_tempAxis != 0 && m_pressFlag)
        {
            m_eventArgs.PressedTime += Time.unscaledDeltaTime;
            EventMgr.InvokeEvent(GameTypeDefine.EVENT_ID.BUTTON_PRESSED, m_eventArgs);
            return true;
        }

        if (m_tempAxis == 0 && m_pressFlag)
        {
            m_pressFlag = false;
            EventMgr.InvokeEvent(GameTypeDefine.EVENT_ID.BUTTON_UP, m_eventArgs);
            m_eventArgs.PressedTime = 0;
        }

        return false;
    }
}
